<template>
  <el-dialog v-model="addQuestionDialog" title="新增题目" width="500px">
    <el-form :model="form" label-width="80px" :rules="rules" ref="formRef">
      <el-form-item label="题目题干" prop="title">
        <el-input type="textarea" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="所属题组" prop="questionGroupId">
        <el-select v-model="form.questionGroupId" placeholder="请选择题组">
          <el-option v-for="(group,index) in groups" :key="index" :label="group.groupName"
                     :value="group.questionGroupId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-select v-model="form.type">
          <el-option label="单选" :value="0"></el-option>
          <el-option label="多选" :value="1"></el-option>
          <el-option label="判断" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <div v-if="form.type !== 2">
        <el-form-item
          v-for="(option, index) in form.options"
          :key="index"
          :label="'选项 ' + (index + 1)"
        >
          <el-input v-model="option.content"></el-input>
          <el-button size="small" type="primary" @click.prevent="removeOption(option)">删除</el-button>
          &nbsp;
          <el-checkbox v-model="form.answers[index]" :label="index" size="large">正确答案</el-checkbox>
        </el-form-item>
      </div>
      <div v-else>
        <el-form-item>
          <el-radio-group v-model="form.answers[0]">
            <el-radio :label="false">错</el-radio>
            <el-radio :label="true">对</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <el-form-item v-if="form.type !== 2">
        <el-button size="small" type="primary" @click.prevent="addOption()">新增</el-button>
      </el-form-item>
      <el-form-item label="分数">
        <el-input-number v-model="form.score"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" :rows="2" type="textarea" placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addQuestionDialog = false">取消</el-button>
        <el-button type="primary" @click="doSubmit(formRef)" :loading="addLoading">确认</el-button>
      </span>
    </template>
  </el-dialog>
  <el-button size="large" @click="addQuestionDialog = true">新增</el-button>
</template>

<script lang="ts" setup>
import { defineProps, inject, reactive, ref } from 'vue'
import { ElForm, ElMessage } from 'element-plus'
import { addQuestion } from '@/api/admin'

interface Option {
  content: string,
  order: number
}

const reload: any = inject('reload')
const addLoading = ref<boolean>(false)
const addQuestionDialog = ref<boolean>(false)
const props = defineProps<{
  groupId: number,
  groups: any
}>()
const form = reactive<{
  title: string,
  remark: string,
  questionGroupId: number,
  score: number,
  options: Option[],
  type: number,
  answers: boolean[]
}>({
  title: '',
  remark: '',
  questionGroupId: props.groupId,
  score: 4,
  options: [{
    content: '',
    order: 0
  }],
  type: 0,
  answers: [false]
})
const rules = {
  title: [{
    required: true,
    message: '请输入题目题干',
    trigger: 'change'
  }]
}
type FormInstance = InstanceType<typeof ElForm>
const formRef = ref<FormInstance>()
const doSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      form.options.forEach(option => {
        option.order = form.options.indexOf(option)
      })
      addLoading.value = true
      addQuestion(form).then(() => {
        ElMessage.success('新增成功')
        addQuestionDialog.value = false
        reload()
      }).finally(() => {
        addLoading.value = false
      })
    } else {
      return false
    }
  })
}
const removeOption = (item: Option) => {
  const index = form.options.indexOf(item)
  if (index !== -1) {
    form.options.splice(index, 1)
    form.answers.splice(index, 1)
  }
}
const addOption = () => {
  form.options.push({
    content: '',
    order: 0
  })
  form.answers.push(false)
}
</script>

<style scoped>

</style>
